package com.naicheng.ivote.engin;

import java.util.HashMap;

import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.doman.BaseRequestBean;
import com.naicheng.ivote.utils.AppManager;
import com.naicheng.ivote.volley.Request;
import com.naicheng.ivote.volley.Response.ErrorListener;
import com.naicheng.ivote.volley.Response.Listener;
import com.naicheng.ivote.volley.VolleyError;
import com.naicheng.ivote.volley.toolbox.JsonObjectRequest;

public abstract class BaseActivity extends Activity implements
		View.OnClickListener {

	protected ProgressDialog mpDialog;
	protected boolean is_alert_request_dialog = true;
	public boolean activityStatus = false;
	protected static final String TAG = "BaseAnimActivity";

	// protected boolean isclosed = true;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setConentView();
		init();
		setOnClickListener();
		processLogic();

		AppManager.getAppManager().addActivity(this);
		// setActivityStatus(false);
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onStop() {
		super.onStop();
		// setActivityStatus(true);
		// MicroFinaceApp.isStatus = true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// closeDialog();
		// Constants.RELOGIN_TIMES = 0;
		AppManager.getAppManager().finishActivity(this);
	}

	/*
	 * public boolean isActivityStatus() { return activityStatus; }
	 * 
	 * public void setActivityStatus(boolean activityStatus) {
	 * this.activityStatus = activityStatus; }
	 */

	@Override
	protected void onRestart() {
		super.onRestart();
		// setActivityStatus(false);
	}

	/**
	 * 加载布局xml文件
	 */
	protected void setConentView() {

	}

	/**
	 * 初始化view控件
	 */
	protected void init() {

	}

	/**
	 * 逻辑数据处理
	 */
	protected void processLogic() {

	}

	/**
	 * 弹出Toast
	 * 
	 * @param content
	 */
	protected void alertToast(String content) {
		Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT)
				.show();
	}

	/**
	 * 设置监听事件
	 */
	protected void setOnClickListener() {

	}

	@Override
	public void onClick(View v) {

	}

	/**
	 * 网络请求
	 * @param requestcommand
	 * @param map
	 * @param listener
	 * @param context
	 */
	public static void getDataFromServer(String requestcommand,
			HashMap<String, String> map, Listener<JSONObject> listener,final Context context) {
		
		ErrorListener errorListener = new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				Toast.makeText(context, "网络不给力呀！请重试！", Toast.LENGTH_LONG).show();
			}
		};

		BaseRequestBean bean = new BaseRequestBean(requestcommand);
		if (map == null) {
			map = new HashMap<String, String>();
		}
		map.putAll(bean.toMap());
		JsonObjectRequest jor = new JsonObjectRequest(Request.Method.POST, ConstantMine.url,
				new JSONObject(map), listener, errorListener);
		MyRequestQueen.getQueenInstance(context).addTask(jor);
	}
	
	public static void showInfoDialog(Context context, String message,
			String titleStr, String positiveStr,
			DialogInterface.OnClickListener onClickListener) {
		AlertDialog.Builder localBuilder = new AlertDialog.Builder(context);
		localBuilder.setTitle(titleStr);
		localBuilder.setMessage(message);
		if (onClickListener == null)
			onClickListener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			};
		localBuilder.setPositiveButton(positiveStr, onClickListener);
		localBuilder.create().show();

	}

}
